/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ipccenter.beans;

import ru.ipccenter.entities.Article;
import ru.ipccenter.entities.Human;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

/**
 *
 * @author sm-ni_000
 */
@Stateless
public class ArticleBean implements ArticleBeanLocal {

    @PersistenceContext(unitName = "WarehousePU", type = PersistenceContextType.TRANSACTION)
    private EntityManager em;
    
    @Override
    public void create(Article entity) {
        em.persist(entity);
    }

    @Override
    public void delete(Article entity) {
        em.remove(entity);
    }

    @Override
    public void save(Article entity) {
        em.merge(entity);
    }

    @Override
    public Article find(Object id) {
        return em.find(Article.class, id);
    }

    @Override
    public Human findAuthor(String title) {
        return (Human)em.createNamedQuery("Article.findAuthor").setParameter("title", title).getSingleResult();
    }

    @Override
    public List<Article> findAllByAuthor(Human author) {
        return (List<Article>)em.createNamedQuery("Article.findAllByAuthor").setParameter("author", author).getResultList();
    }

    @Override
    public List<Article> findByDate(Timestamp beginDate, Timestamp endDate) {
        return (List<Article>)em.createNamedQuery("Article.findByDate").setParameter("beginDate", beginDate).setParameter("endDate", endDate).getResultList();
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
